﻿Imports Infragistics.Win
Imports System.Drawing
Imports System.IO
Imports System.Drawing.Imaging

Public Class hDCommonFunction

    ''' <summary>
    ''' Hàm dùng để convert một ảnh sang dạng nhị phân
    ''' </summary>
    ''' <param name="sFileName">đường dẫn ảnh</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ReadFile(ByVal sFileName As String) As Byte()
        Dim fs As System.IO.FileStream = New System.IO.FileStream(sFileName, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Read)
        Dim rs() As Byte = New Byte(fs.Length) {}
        fs.Read(rs, 0, System.Convert.ToInt32(fs.Length))
        fs.Close()
        Return rs
    End Function

    Public Function ReadImage(ByVal iImage_ As Image) As Byte()
        Dim ms As MemoryStream = New MemoryStream()
        iImage_.Save(ms, ImageFormat.Jpeg)
        Dim bmpBytes As Byte() = ms.GetBuffer()
        iImage_.Dispose()
        ms.Close()
        Return bmpBytes
    End Function

    ''' <summary>
    ''' Hàm convert từ một hình từ dạng nhị phân sang dạng ảnh thông thường
    ''' </summary>
    ''' <param name="aImage">Chuỗi nhị phân</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function LoadFile(ByVal aImage() As Byte) As System.Drawing.Image
        Dim ms As New System.IO.MemoryStream(aImage)
        Dim bm As New System.Drawing.Bitmap(ms)
        ms.Close()
        Return bm
    End Function

    Public Function InviColumns(ByVal sArray() As String, ByVal ods As Data.DataSet, ByVal sTableName As String) As DataSet
        Dim sColumnsName As String
        For Each sColumnsName In sArray
            ods.Tables(sTableName).Columns(sColumnsName).ColumnMapping = MappingType.Hidden
        Next
        Return ods
    End Function

    Public Function CopyFille(ByVal fileToCopy As String) As String
        Dim newPath As String = PathApplication + "HopDongLaoDong\" + Path.GetFileName(fileToCopy)
        Try
            If System.IO.File.Exists(fileToCopy) = True Then
                System.IO.File.Copy(fileToCopy, newPath)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        Return newPath
    End Function

    Public Sub DeleteFile(ByVal fileDelete As String)
        Try
            If System.IO.File.Exists(fileDelete) = True Then
                System.IO.File.Delete(fileDelete)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Public Function ReadFiletoByte(ByVal path As String) As Byte()
        Dim data As Byte()
        Try
            Dim fs As New FileStream(path, FileMode.Open, FileAccess.Read)
            Dim br As New BinaryReader(fs)
            Data = br.ReadBytes(fs.Length)
            br.Close()
            fs.Close()
        Catch
            MessageBox.Show("Lỗi không thể đọc file tài liệu!", Application.ProductName)
            Return Nothing
        End Try
        Return data
    End Function

    Public Function XuLyThoiGian(ByVal oControlTuNgay As Infragistics.Win.UltraWinEditors.UltraDateTimeEditor, ByVal oControlDenNgay As Infragistics.Win.UltraWinEditors.UltraDateTimeEditor, ByVal cboChon As Infragistics.Win.UltraWinEditors.UltraComboEditor) As Date
        Dim a As Integer = Now.Day 'ngày
        Dim b As Integer = Now.DayOfWeek  'ngày của tuần
        Dim c As Integer = Now.Month 'tháng
        Dim dtBatDau As Date
        Dim dtKetThuc As Date
        'hôm nay
        If cboChon.Value = 0 Then
            dtBatDau = Now.Date
            dtKetThuc = Now.Date
        End If
        'tuần này
        If cboChon.Value = 1 Then
            dtBatDau = Now.Date.AddDays(-b + 1)
            dtKetThuc = Now.Date.AddDays(-b + 7)
        End If
        'tuần này đến hiện tại
        If cboChon.Value = 2 Then
            dtBatDau = Now.Date.AddDays(-b + 1)
            dtKetThuc = Now.Date
        End If
        'tháng này
        If cboChon.Value = 3 Then
            dtBatDau = Now.Date.AddDays(-a + 1)
            dtKetThuc = Now.Date.AddMonths(1).AddDays(-Now.Day)
        End If
        'tháng này đến hiện tại
        If cboChon.Value = 4 Then
            dtBatDau = Now.Date.AddDays(-a + 1)
            dtKetThuc = Now.Date
        End If
        'quý này
        If cboChon.Value = 5 Then
            If c < 4 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 1)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(3)
            ElseIf c < 7 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 4)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(6)
            ElseIf c < 10 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 7)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(9)
            ElseIf c <= 12 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 10)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(12)
            End If
        End If
        'quý này đến hiện tại
        If cboChon.Value = 6 Then
            If c < 4 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 1)
                dtKetThuc = Now.Date
            ElseIf c < 7 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 4)
                dtKetThuc = Now.Date
            ElseIf c < 10 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 7)
                dtKetThuc = Now.Date
            ElseIf c <= 12 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 10)
                dtKetThuc = Now.Date
            End If
        End If
        'năm nay
        If cboChon.Value = 7 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 1)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(12)
        End If
        'năm nay đến hiện tại
        If cboChon.Value = 8 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 1)
            dtKetThuc = Now.Date
        End If
        'tháng 1
        If cboChon.Value = 9 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 1)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(1)
        End If
        'tháng 2
        If cboChon.Value = 10 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 2)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(2)
        End If
        'tháng 3
        If cboChon.Value = 11 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 3)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(3)
        End If
        'tháng 4
        If cboChon.Value = 12 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 4)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(4)
        End If
        'tháng 5
        If cboChon.Value = 13 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 5)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(5)
        End If
        'tháng 6
        If cboChon.Value = 14 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 6)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(6)
        End If
        'tháng 7
        If cboChon.Value = 15 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 7)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(7)
        End If
        'tháng 8
        If cboChon.Value = 16 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 8)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(8)
        End If
        'tháng 9
        If cboChon.Value = 17 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 9)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(9)
        End If
        'tháng 10
        If cboChon.Value = 18 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 10)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(10)
        End If
        'tháng 11
        If cboChon.Value = 19 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 11)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(11)
        End If
        'tháng 12
        If cboChon.Value = 20 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 12)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(12)
        End If
        'quý 1
        If cboChon.Value = 21 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 1)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(3)
        End If
        'quý 2
        If cboChon.Value = 22 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 4)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(6)
        End If
        'quý 3
        If cboChon.Value = 23 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 7)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(9)
        End If
        'quý 4
        If cboChon.Value = 24 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 10)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(12)
        End If
        'tuần trước
        If cboChon.Value = 25 Then
            dtBatDau = Now.Date.AddDays(-b + 1).AddDays(-7)
            dtKetThuc = Now.Date.AddDays(-b + 7).AddDays(-7)
        End If
        'tháng trước
        If cboChon.Value = 26 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-1)
            dtKetThuc = Now.Date.AddMonths(1).AddDays(-Now.Day).AddMonths(-1)
        End If
        'quý trước
        If cboChon.Value = 27 Then
            If c < 4 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 1).AddMonths(-3)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(3).AddMonths(-3)
            ElseIf c < 7 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 4).AddMonths(-3)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(6).AddMonths(-3)
            ElseIf c < 10 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 7).AddMonths(-3)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(9).AddMonths(-3)
            ElseIf c <= 12 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 10).AddMonths(-3)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(12).AddMonths(-3)
            End If
        End If
        'năm trước
        If cboChon.Value = 28 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 1).AddYears(-1)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(12).AddYears(-1)
        End If
        'tuần sau
        If cboChon.Value = 29 Then
            dtBatDau = Now.Date.AddDays(-b + 1).AddDays(7)
            dtKetThuc = Now.Date.AddDays(-b + 7).AddDays(7)
        End If
        'bốn tuần sau
        If cboChon.Value = 30 Then
            dtBatDau = Now.Date.AddDays(-b + 1).AddDays(7)
            dtKetThuc = Now.Date.AddDays(-b + 7).AddDays(28)
        End If
        'tháng sau
        If cboChon.Value = 31 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(1)
            dtKetThuc = Now.Date.AddMonths(1).AddDays(-Now.Day).AddMonths(1)
        End If
        'quý sau
        If cboChon.Value = 32 Then
            If c < 4 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 1).AddMonths(3)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(3).AddMonths(3)
            ElseIf c < 7 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 4).AddMonths(3)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(6).AddMonths(3)
            ElseIf c < 10 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 7).AddMonths(3)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(9).AddMonths(3)
            ElseIf c <= 12 Then
                dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 10).AddMonths(3)
                dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(12).AddMonths(3)
            End If
        End If
        'năm sau
        If cboChon.Value = 33 Then
            dtBatDau = Now.Date.AddDays(-a + 1).AddMonths(-Now.Month + 1).AddYears(1)
            dtKetThuc = Now.Date.AddMonths(-Now.Month + 1).AddDays(-Now.Day).AddMonths(12).AddYears(1)
        End If
        If cboChon.Value = 34 Then
            dtBatDau = Now.Date
            dtKetThuc = Now.Date
        End If
        oControlTuNgay.Value = dtBatDau
        oControlDenNgay.Value = dtKetThuc
    End Function

End Class
